<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IDepartmentAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';

	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

 	class   MySqlDepartmentAccessDao implements IDepartmentAccessDao {
		/**
		 * Constructor
		 */
		public function __construct () {}
	 
  

     public function createDepartment ($department) {

		$name = $department -> getName();
	    $fkRifBranch = $department -> getFkRifBranch();
	
		$connection = ConnectionFactory::getMySqlConnectionAccessDao();
		$conn=$connection->openConnection();
	
		 $GLOBALS['debug'] -> log(" LLEGO AL DAO DE DEPARTMENT");
	 	 $GLOBALS['debug'] -> log($department,"datos a insertar en departamento");
	  
	  	$query = "INSERT INTO DEPARTMENT 
		           VALUES (null,'$name','$fkRifBranch')";
	 	$GLOBALS['debug'] -> log($query,"QUERY");
	 
	 
	    $sentence=mysql_query($query,$conn);
	       if ($sentence){
			    return true;
			   }
		  return false;
	 
	 }
	
		 
		/**
 		 * Selects departments by branch rif
		 */
		public function selectDepartmentsByBranchRif($rif) {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT pk_id, name
					  FROM DEPARTMENT
					  WHERE fk_rif_branch = '$rif'";
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
			
			return $sentence;
		}
		
		/**
 		 * Selects departments
		 */
		public function selectDepartments() {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
				$GLOBALS['debug']->log('INICIO Funcion DAO LISTA DE DEPARTAMENTOS');
				
			$query = "SELECT d.name as name_department,b.name as name_branch,b.pk_rif as pk_rif
					  FROM DEPARTMENT d, Branch b
					  WHERE d.fk_rif_branch=b.pk_rif ";
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
				$GLOBALS['debug']->log('FIN Funcion DAO LISTA DE DEPARTAMENTOS');
			$GLOBALS['debug']->log($query, "Query departments");
			return $sentence;
		}
		
		
	}

?>